home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / BorderPanel.java < prev    next >
Text File  |  1998-09-15  |  4KB  |  121 lines

  1. /*
  2.  * @(#)BorderPanel.java    1.1 98/06/16
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31.  
  32. import java.awt.*;
  33.  
  34. public class BorderPanel extends Panel
  35. {
  36.    /**
  37.     * Panel shadow border width
  38.     */
  39.    protected int shadow = 4;
  40.  
  41.    /**
  42.     * Panel raised vs depressed look
  43.     */
  44.    protected boolean raised = true;
  45.     public BorderPanel() {
  46.         this.raised=true;
  47.     }
  48.  
  49.     public BorderPanel(boolean raised) {
  50.         this.raised=raised;
  51.     }
  52.  
  53.  
  54.    /**
  55.     * Re-layout parent. Called when a panel changes
  56.     * size etc.
  57.     */
  58.    protected void layoutParent() {
  59.       Container parent = getParent();
  60.       if (parent != null) {
  61.      parent.doLayout();
  62.       }
  63.    }
  64.  
  65.    public void paint(Graphics g) {
  66.         super.paint(g);
  67.         Dimension size = getSize();
  68.         paintBorder(g, size);
  69.     }
  70.  
  71.    protected void paintBorder(Graphics g, Dimension size) {
  72.       Color c = getBackground();
  73.       g.setColor(c);
  74.       g.fillRect(0, 0, size.width, size.height);
  75.       draw3DRect(g, 0, 0, size.width, size.height, raised);
  76.    }
  77.  
  78.    /**
  79.     * Draw a 3D Rectangle.
  80.     * @param g the specified Graphics window
  81.     * @param x, y, width, height
  82.     * @param raised - true if border should be painted as raised.
  83.     * @see #paint
  84.     */
  85.    public void draw3DRect(Graphics g, int x, int y, int width, int height,
  86.               boolean raised) {
  87.       Color c = g.getColor();
  88.       Color brighter = avgColor(c,Color.white);
  89.       Color darker = avgColor(c,Color.black);
  90.  
  91.  
  92.       // upper left corner
  93.       g.setColor(raised ? brighter : darker);
  94.       for (int i=0; i<shadow; i++) {
  95.       g.drawLine(x+i, y+i, x+width-1-i, y+i);
  96.       g.drawLine(x+i, y+i, x+i, y+height-1-i);
  97.       }
  98.       // lower right corner
  99.       g.setColor(raised ? darker : brighter);
  100.       for (int i=0; i<shadow; i++) {
  101.       g.drawLine(x+i, y+height-1-i, x+width-1-i, y+height-1-i);
  102.       g.drawLine(x+width-1-i, y+height-1-i, x+width-1-i, y+i);
  103.       }
  104.       g.setColor(c);
  105.       // added by rip.
  106.       g.setColor(Color.black);
  107.       g.drawRect(x,y,width+2,height+2);
  108.  
  109.    }
  110.  
  111.    public static Color avgColor(Color c1, Color c2) {
  112.     return new Color(
  113.         (c1.getRed()+c2.getRed())/2,
  114.         (c1.getGreen()+c2.getGreen())/2,
  115.         (c1.getBlue()+c2.getBlue())/2
  116.         );
  117.    }
  118.  
  119. }
  120.  
  121.